import { defineComponent, computed, createVNode, Fragment } from 'vue';
import Input from '../Input/index.js';
import formFieldRef from '../../use/formFieldRef.js';
import { FeatherChevronUp, FeatherChevronDown } from 'cui-vue-icons/feather';

const index = /* @__PURE__ */ defineComponent({
  name: 'Spinner',
  props: {
    size: {
      type: String
    },
    name: {
      type: String
    },
    modelValue: {
      type: Number
    },
    max: {
      type: Number
    },
    min: {
      type: Number
    },
    step: {
      type: Number
    },
    loop: {
      type: Boolean
    },
    placeholder: {
      type: String
    },
    disabled: {
      type: Boolean
    },
    asFormField: {
      type: Boolean,
      default: true
    }
  },
  emits: ['change', 'plus', 'sub', 'update:modelValue'],
  setup(props, {
    emit
  }) {
    const classList = computed(() => ({
      'cm-spinner': true,
      [`cm-spinner-${props.size}`]: props.size,
      'cm-spinner-disabled': props.disabled
    }));
    const value = formFieldRef(props, emit, Math.max(0, props.min ?? 0));
    const _onInput = (val, e) => {
      val = val.replace(/[^0-9\.]/g, '');
      e.target.value = val;
    };
    const _onKeyDown = e => {
      if (e.keyCode === 38) {
        plus();
      }
      if (e.keyCode === 40) {
        sub();
      }
    };
    const min = props.min || 0;
    const step = props.step || 1;
    const _onChange = v => {
      let val = v;
      if (props.max !== undefined) {
        val = Math.min(val, props.max);
      }
      if (min !== undefined) {
        val = Math.max(val, min);
      }
      Promise.resolve().then(() => {
        value.value = Number(val);
      });
      emit('change', val);
    };

    /**
     * 增加
     * @memberof Spinner
     */
    const plus = () => {
      if (props.disabled) {
        return;
      }
      let v = add(value.value, step);
      if (props.loop && props.max !== undefined && min !== undefined && v > props.max) {
        const off = v - props.max;
        v = min + off - 1;
      }
      if (props.max !== undefined) {
        v = Math.min(props.max, v);
      }
      value.value = v;
      emit('change', v);
      emit('plus', v, step);
    };

    /**
     * 减少
     * @memberof Spinner
     */
    const sub = () => {
      if (props.disabled) {
        return;
      }
      let v = add(value.value, -step);
      if (props.loop && props.max !== undefined && min !== undefined && v < min) {
        const off = v - min;
        v = props.max + off + 1;
      }
      if (min !== undefined) {
        v = Math.max(min, v);
      }
      value.value = v;
      emit('change', v);
      emit('sub', v, step);
    };

    /**
     * 解决数字浮点型双精度问题
     * @param {[type]} num1 [description]
     * @param {[type]} num2 [description]
     */
    function add(num1, num2) {
      let r1;
      let r2;
      try {
        r1 = num1.toString().split('.')[1].length;
      } catch (e) {
        r1 = 0;
      }
      try {
        r2 = num2.toString().split('.')[1].length;
      } catch (e) {
        r2 = 0;
      }
      const m = Math.pow(10, Math.max(r1, r2));
      return (num1 * m + num2 * m) / m;
    }
    return () => createVNode("div", {
      "class": classList.value
    }, [createVNode(Input, {
      "size": props.size,
      "placeholder": props.placeholder,
      "disabled": props.disabled,
      "onInput": _onInput,
      "asFormField": false,
      "modelValue": value.value,
      "onChange": _onChange,
      "onKeyDown": _onKeyDown,
      "append": createVNode(Fragment, null, [createVNode("span", {
        "class": "cm-spinner-plus",
        "onClick": plus
      }, [createVNode(FeatherChevronUp, null, null)]), createVNode("span", {
        "class": "cm-spinner-subs",
        "onClick": sub
      }, [createVNode(FeatherChevronDown, null, null)])])
    }, null)]);
  }
});

export { index as default };
